<?php
FLEA :: loadClass('Controller_Base');

class Controller_Default extends Controller_Base {
	function actionIndex() {
		$this->show();
	}

	function actionPractitionerLogin(){
		$this->show();	
	}
	
	function actionDoForForgetPassword(){
		$email	=	$_GET['Email'];
		
		$_modelUser	=&	FLEA::getSingleton("Model_User");
		$user		=	$_modelUser->findByField('Email',$email);
		
		if($user['Email'] == $email){
			$message	=	"Your password for Ingenuity Nexus is ".$user['Password'].' .<br> Thank you.';
			
			$mailsubject = "[noreply@ingenuitynexus.com] ------";
			$mailHeader = "Content-Type:text/HTML ";
		
			$this->sendMail($email,$mailsubject,$message,$mailHeader);
			print 'We send a email to tell you your password ! ';
		}
		else{
			print 'This user is not existed !';
		}
	}
	function actionSearchInventions() {
		$_modelInvention = & FLEA :: getSingleton("Model_Invention");
		$keyWords	=	htmlentities($_POST['keywords']);
		
		$keyWords	=	str_replace("'","\'",$keyWords);
		$keyWords	=	str_replace('"','\"',$keyWords);
		$inventions	=	$_modelInvention->searchInventions($keyWords);
		$this->viewdata['inventions'] = $inventions;

		$this->viewdata['keyWords']	=	$keyWords;
		$this->show();
	}

	function actionShowInvention() {
		$inventionID = $_GET['InventionID'];
		if ($inventionID == '') {
			$this->showResult("No Invention");
			exit (0);
		}

		$_modelInvention = & FLEA :: getSingleton("Model_Invention");
		$invention = $_modelInvention->findInvention($inventionID);
		$this->viewdata['invention'] = $invention;

		$_modelAction = & FLEA :: getSingleton("Model_Action");
		$actions = $_modelAction->findActions($inventionID);
		$this->viewdata['actions'] = $actions;
		//dump($actions);
		$this->show();
	}

	function actionDoLogin() {
		$_modelUser = & FLEA :: getSingleton('Model_User');
		$email = $_POST['Email'];
		$password = $_POST['Password'];

		$user = $_modelUser->userLogin($email, $password);

		if ($user['UserID'] == '') {
			$role = 0;
		} else
			if ($user['Password'] != $password) {
				$role = -1;
			} else
				if ($user['IPManager'] == 1) {
					$role = 5;
				} else {
					if ($user['ClientID'] != '') {
						$role = 1;
					} else
						if ($user['InventorID'] != '') {
							/**
							 * Ok! Go inventor screen.....
							 */
							$role = 2;
						} else
							if ($user['FirmID'] != '') {
								$role = 3;
							} else
								if ($user['PractitionerID'] != '') {
									$role = 4;
								}
				}
		
		switch ($role) {
			case -1 :
				$result	= "The password is not correct!";
				break;
			case 0 :
				$result	= "The user is not exist!";
				break;
			case 1 :
				$result = "Successfully logged in , you are in the client screen!";
				break;
			case 2 :
				$result = "Successfully logged in , you are in the inventor screen!";
				break;
			case 3 :
				$result = "Successfully logged in , you are in the firm screen!";
				break;
			case 4 :
				$result = "Successfully logged in , you are in the practitioner screen!";
				break;
			case 5 :
				$result = "Successfully logged in , you are in the manager screen!";
				break;
		}
		
		if($role > 0)
		{
			session_start();
			
			if(!session_is_registered("UserID")){
				if(session_register("UserID")){
					$UserID = $user['UserID'];
				}
			}
			if(!session_is_registered("Role")){
				if(session_register("Role")){
					$Role	=	$role;
				}
			}
		}
		
		
			$this->viewdata['url']	=	APP_NAME.'/main.html';
		
		$title	=	"Result of login";
		$this->viewdata['title']	=	$title;
		$this->viewdata['result']	=	$result;
		$this->showResultView();
	}

	function actionViewInvention() {
		$inventionID = $_GET["InventionID"];
		if ($inventionID == "") {
			exit (0);
		}

		$_modelInvention = & FLEA :: getSingleton('Model_Invention');
		$invention = $_modelInvention->findInvention($inventionID);
		$this->viewdata['invention'] = $invention;

		$_modelAction = & FLEA :: getSingleton('Model_Action');
		$actions = $_modelAction->findActions($inventionID);
		$this->viewdata['actions'] = $actions;
		$this->_executeView(View_DIR . DS . "ViewInvention.php", $this->viewdata);
	}

	function actionAboutUs() {
		$this->show();
	}

	function actionViewAction() {
		$actionID = $_GET["ActionID"];
		if ($actionID == "") {
			exit (0);
		}

		$_modelAction = & FLEA :: getSingleton('Model_Action');
		$action1 = $_modelAction->findAction($actionID);
		$this->viewdata['action1'] = $action1;

		$this->_executeView(View_DIR . DS . 'Invention' . DS . "ActionTem.php", $this->viewdata);
	}

	function actionShowApplication() {
		include ('main.html');
		
	}
}
?>